﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace UCLib
{
    public partial class UC_ListBox : UserControl
    {
        public UC_ListBox()
        {
            InitializeComponent();

            currentPageID = 1;

            ItemsCount = GetItemsCount();

            Lenlimited = 100;

            PageSize = 50;

            PageCount = 0;

            
        }

        IList<string> strList;

        int currentPageID = 1;

        int ItemsCount;

        int Lenlimited;

        int PageSize;

        int PageCount;

        public IList<string> GetList()
        {
            return strList;
        }

        public void SetList(IList<string> list)
        {
            strList = list;

            ItemsCount = GetItemsCount();

            currentPageID = 1;

            PageCount = GetPageCount(ItemsCount, PageSize);


            PaintList(strList, currentPageID);
        }

        private void PaintList(IList<string> list, int pageID)
        {
            textBox_Page.Text = "";

            int startID = GetStartID(pageID, PageCount, PageSize, ItemsCount);

            int endID = GetEndID(pageID, PageCount, PageSize, ItemsCount);

            for (int i = startID; i < endID; i++)
            {
                //if (list[i].Length <= Lenlimited)
                //{
                textBox_Page.Text += list[i];
                if (i!=endID-1)
                {
                    textBox_Page.Text += "\r\n";
                } 
                //}
            }

            label_PageNum.Text = pageID.ToString() + "/" + PageCount.ToString();

            label_ItemsCount.Text = list.Count.ToString() + " : 项";
        }


        private int GetEndID(int pageID, int PageCount, int PageSize, int ItemsCount)
        {
            int EndID = GetStartID(pageID, PageCount, PageSize, ItemsCount) + PageSize;

            if (EndID > ItemsCount)
            {
                return ItemsCount;
            }
            return EndID;
        }

        private int GetStartID(int pageID, int PageCount, int PageSize, int ItemsCount)
        {
            if (pageID <= 0)
            {
                return 0;
            }

            if (pageID == 1)
            {
                return 0;
            }

            if (pageID > PageCount)
            {
                return GetStartID(PageCount, PageCount, PageSize, ItemsCount);
            }

            int StratID = (pageID - 1) * PageSize;

            return StratID;
        }

        private int GetItemsCount()
        {
            if (strList != null)
            {
                return strList.Count;
            }
            return 0;
        }

        private int GetPageCount(int count, int PageSize)
        {
            int PageCount = count / PageSize;

            if (count % PageSize != 0)
            {
                PageCount++;
            }

            return PageCount;
        }

        private void button_BOL_Click(object sender, EventArgs e)
        {
            currentPageID = 1;
            PaintList(strList, currentPageID);
        }

        private void button_EOL_Click(object sender, EventArgs e)
        {
            currentPageID = PageCount;
            PaintList(strList, currentPageID);
        }

        private void button_PageUp_Click(object sender, EventArgs e)
        {
            currentPageID--;

            if (currentPageID < 1)
            {
                currentPageID = 1;
            }

            PaintList(strList, currentPageID);
        }

        private void button_PageDown_Click(object sender, EventArgs e)
        {
            currentPageID++;

            if (currentPageID > PageCount)
            {
                currentPageID = PageCount;
            }

            PaintList(strList, currentPageID);
        }

        private void button_Go_Click(object sender, EventArgs e)
        {
            int PageNumToGo = GetPageNumToGo();

            if (PageNumToGo>PageCount)
            {
                PageNumToGo = PageCount;
            }
            else if (PageNumToGo <1)
            {
                PageNumToGo = 1;
            }

            currentPageID = PageNumToGo;

            PaintList(strList, currentPageID);
        }

        private int GetPageNumToGo()
        {
            try
            {
                return int.Parse(textBox_PageNum.Text.Trim());
            }
            catch 
            {
                return 0;
            }
            
        }


        public void Clear()
        {
            strList = new List<string>();

            ItemsCount = 0;

            currentPageID = 0;

            PageCount =0;

            textBox_Page.Text = "";

            label_PageNum.Text ="0/0";

            label_ItemsCount.Text = "0:项";
        }

        private void button_load_Click(object sender, EventArgs e)
        {
            AddToUI(DicLoader.GetFilesList());
        }

        private void AddToUI(IList<string> paramsList)
        {
            if (paramsList == null)
            {
                return;
            }
            SetList(paramsList);
        }

        public void AddToList(IList<string> newItems)
        {
            if (strList==null)
            {
                strList = new List<string>();
            }
            foreach (string item in newItems)
            {
                strList.Insert(0, item);
            }
            SetList(strList);
        }

        private void button_Manual_Click(object sender, EventArgs e)
        {
            FrmInput frmInput = new FrmInput();
            if (frmInput.ShowDialog() == DialogResult.OK)
            {
                AddToList(frmInput.GetList());
            }
        }

        private void button_Clear_Click(object sender, EventArgs e)
        {
            Clear();
        }
    }
}
